{% macro menu_icon(item) -%}
{% set icon_type = item.get_icon_type() %}
{%- if item.get_icon_value() %}
  {% if not icon_type %}{% set icon_type = 'fa' %}{% endif %}
  {% set icon_value = item.get_icon_value() %}
  {% if icon_type == 'glyph' %}
    <i class="glyphicon {{ icon_value }}"></i>
  {% elif icon_type == 'fa' %}
    <i class="fa fa-{{ icon_value }}"></i>
  {% elif icon_type == 'image' %}
    <img src="{{ url_for('static', filename=icon_value) }}" alt="menu image">
  {% elif icon_type == 'image-url' %}
    <img src="item.icon_value" alt="menu image">
  {% endif %}
{% endif %}
{%- endmacro %}

{% macro menu() %}
  {%- for item in admin_view.admin.menu() %} 
    {%- if item.name == '首页' -%}
    {%- elif item.is_category() -%}
      {% set children = item.get_children() %}
      {%- if children %}
        {% set class_name = item.get_class_name() %}
        <li>
        {%- if item.is_active(admin_view) %}
          <a class="active" href="javascript:void(0)">{% if item.class_name %}<i class="{{ item.class_name }}"></i> {% endif %}{{ item.name }}<span class="fa arrow"></span></a>
        {% else -%}
          <a href="javascript:void(0)">{% if item.class_name %}<i class="{{ item.class_name }}"></i> {% endif %}{{ item.name }}<span class="fa arrow"></span></a>
        {%- endif %}
            <ul class="nav nav-second-level">
                {%- for child in children -%}
                  {% set class_name = child.get_class_name() %}
                  <li{% if class_name %} class="{{class_name}}"{% endif %}>
                  {%- if child.is_active(admin_view) %}
                    <a class="active" href="{{ child.get_url() }}">{{ menu_icon(child) }}{{ child.name }}</a>
                  {% else %}
                    <a href="{{ child.get_url() }}">{{ menu_icon(child) }}{{ child.name }}</a>
                  {%- endif %}
                  </li>
                {%- endfor %}
            </ul>
        </li>
      {% endif %}
    {%- else %}
      {%- if item.is_accessible() and item.is_visible() -%}
        {% set class_name = item.get_class_name() %}
        <li{% if class_name %} class="{{class_name}}"{% endif %}>
        {%- if item.is_active(admin_view) %}
          <a class="active" href="{{ item.get_url() }}">{{ menu_icon(item) }}{{ item.name }}</a>
        {%- else %}
          <a href="{{ item.get_url() }}">{{ menu_icon(item) }}{{ item.name }}</a>
        {%- endif %}
        </li>
      {%- endif -%}
    {% endif -%}
  {% endfor %}
{% endmacro %}

{% macro menu_links() %}
  {% for item in admin_view.admin.menu_links() %}
    {% if item.is_accessible() and item.is_visible() %}
      <li>
        <a href="{{ item.get_url() }}">{{ menu_icon(item) }}{{ item.name }}</a>
      </li>
    {% endif %}
  {% endfor %}
{% endmacro %}

{% macro messages() %}
  {% with messages = get_flashed_messages(with_categories=True) %}
    {% if messages %}
      {% for category, m in messages %}
        {% if category %}
        {# alert-error changed to alert-danger in bootstrap 3, mapping is for backwards compatibility #}
        {% set mapping = {'message': 'info', 'error': 'danger'} %}
        <div class="alert alert-{{ mapping.get(category, category) }} alert-dismissable">
        {% else %}
        <div class="alert alert-dismissable">
        {% endif %}
          <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
          {{ m }}
        </div>
      {% endfor %}
    {% endif %}
  {% endwith %}
{% endmacro %}
